package org.jeecg.modules.compilation.util;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.Objects;

/**
 * 文件夹工具类
 */
@Slf4j
public class FolderUtil {
    /**
     * 创建文件夹
     */
    public static Boolean createFolder(String absolutePath){
        absolutePath = replaceSeparator(absolutePath);
        File folder = new File(absolutePath);

        if(!folder.exists() || !folder.isDirectory()){
            if(folder.mkdirs()){
                log.info("create folder \"" + absolutePath + "\" successfully!");
                return true;
            }
        }

        log.info("create folder \"" + absolutePath + "\" failed because it has exists!");
        return false;
    }

    /**
     * 重命名文件夹
     * 因为linux与win的差异，需要把/换成当前系统的分隔符
     */
    public static Boolean renameFolder(String oldRelativePath, String newRelativePath){
        String oldAbsolutePath = replaceSeparator(setAbsolutePath(oldRelativePath));
        String newAbsolutePath = replaceSeparator(setAbsolutePath(newRelativePath));
        File oldFolder = new File(oldAbsolutePath);

        if(oldFolder.exists() && oldFolder.isDirectory()){
            //名称没改变，无需改变文件夹名称
            if(oldAbsolutePath.equals(newAbsolutePath)){
                log.info("folder \"" + newAbsolutePath + "\" has already exists!");
                return true;
            }
            //名称改变，尝试移动到新文件夹
            try{
                String[] fileList = oldFolder.list();
                assert fileList != null;

                FolderUtil.createFolder(newAbsolutePath);
                for(String uuid : fileList){
//                    ImgOperateUtil.copyImg(oldRelativePath+uuid, newRelativePath+uuid);
                }
                FolderUtil.delFolder(oldAbsolutePath);

                log.info("folder \"" + oldAbsolutePath + "\" rename to \"" + newAbsolutePath + "\" successfully!");
                return  true;

            }catch (Exception e){
                log.error("folder \"" + oldAbsolutePath + "\" is exists and is a directory but rename to \"" + newAbsolutePath + "\" failed!");
                e.printStackTrace();
                return false;
            }finally {
                ;
            }
        }

        log.error("folder \"" + oldAbsolutePath + "\" is not exists or is not a directory!");
        return false;
    }

    /**
     * 绝对路径
     */
    public static String setAbsolutePath(String relativePath){
        //str以\\开头
        if(relativePath.startsWith(Constants.BACK_SLASH)){
            return Constants.CURRENT_DIR + relativePath;
        }

        //以/开头
        return Constants.CURRENT_DIR + relativePath;
    }


    /**
     * 删除文件夹
     */
    public static Boolean delFolder(String relativePath) {
        String absolutePath = relativePath;
        if(!absolutePath.startsWith(Constants.CURRENT_DIR)){
            absolutePath = FolderUtil.replaceSeparator(FolderUtil.setAbsolutePath(relativePath));
        }
        File file = new File(absolutePath);

        if(!file.exists()){
            log.error("\"" + absolutePath + "\" is not exists!");
            return false;
        }

        //是文件，直接删除
        if(file.isFile()){
            return delFile(file);
        }

        String[] fileList = file.list();
        //是空文件夹
        if(file.isDirectory() && Objects.requireNonNull(fileList).length==0){
            return delFile(file);
        }

        //非空文件夹，
        for (int i=0; i<Objects.requireNonNull(fileList).length; i++) {
            String tempFilePath;
            if (absolutePath.endsWith(File.separator)) {
                tempFilePath = absolutePath + fileList[i];
            } else {
                tempFilePath = absolutePath + File.separator + fileList[i];
            }

            //递归
            delFolder(tempFilePath);
        }
        delFile(file);

        return true;
    }

    /**
     * 删除文件夹作中的文件
     */
    public static Boolean delFile(File file){
        if(file.delete()){
            log.info("delete \"" + file.getAbsolutePath() + "\" successfully!");
            return true;
        }

        log.error("delete \"" + file.getAbsolutePath() + "\" failed!");
        return false;
    }

    /**
     * 替换分隔符
     */
    public static String replaceSeparator(String str){
        return str.replace("/", Constants.CURRENT_SEPARATOR);
    }

}





